package me.baicai.weixin.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.baicai.weixin.conf.WeiXinKey;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;


/**
 * @description:
 * @author: [ 超级大白菜 ]
 * @date: 2019-11-09 16:29
 */
public class HttpClient
{

    public final static String getUserUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" +
                                            WeiXinKey.appid + "&secret=" + WeiXinKey.secret +
                                            "&grant_type=authorization_code&js_code=";

    public final static String AccessToken =
        "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
        WeiXinKey.appid + "&secret=" + WeiXinKey.secret;

    public static JSONObject getRequest(String url)
    {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try
        {
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200)
            {
                // 解析响应，获取数据
                return (JSONObject)JSONObject.parse(
                    EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (response != null)
            {
                // 关闭资源
                try
                {
                    response.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            // 关闭浏览器
            try
            {
                httpclient.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String unifiedorder(String reqpest)
    {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(WeiXinKey.unifiedorder);
        try
        {
            StringEntity stringEntity = new StringEntity(reqpest);
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * post请求并得到返回结果
     *
     * @param requestUrl
     * @param output
     * @return
     */
    public static String httpsRequest(String requestUrl, String output)
    {
        try
        {
            URL url = new URL(requestUrl);
            HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            if (null != output)
            {
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(output.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null)
            {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            connection.disconnect();
            return buffer.toString();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return "";
    }

}
